import json
import math
import random
from scipy import integrate
import matplotlib.pyplot as plt
from django.http import JsonResponse
from course.models import *

static_file_path = 'static/files/'


class BaseModel:

    def __init__(self):
        # 0平均 1正态
        self.mode = 0
        self.param1 = 0
        self.param2 = 1

    def set_value(self, params):
        for key in params:
            try:
                setattr(self, key, float(params[key]))
            except Exception:
                pass

    def set_f(self, mode, params):
        self.mode = mode
        if self.mode <= 1:
            self.param1 = params[0]
            self.param2 = params[1]
        else:
            return NotImplementedError

    def f_ni(self, y):
        if self.mode == 0:
            return self.average_f_ni(y)
        elif self.mode == 1:
            return self.uniform_f_ni(y)
        else:
            return NotImplementedError

    def f(self, x):
        if self.mode == 0:
            return self.average_f(x)
        elif self.mode == 1:
            return self.uniform_f(x)
        else:
            return NotImplementedError

    def average_f_ni(self, y):
        if y < 0 or y > 1:
            return ValueError
        else:
            return (1 - y) * (self.param2 - self.param1) + self.param1

    def uniform_f_ni(self, y):
        if y < 0 or y > 1:
            return ValueError
        else:
            left = -1000000
            right = 1000000
            eps = 0.0001
            while left + eps < right:
                mid = (left + right) / 2.0
                if self.uniform_f(mid) < y:
                    left = mid
                else:
                    right = mid
            return left

    def average_f(self, x):
        if x < self.param1:
            return 0
        elif x > self.param2:
            return 1
        else:
            return (x - self.param1) / (self.param2 - self.param1)

    def uniform_f(self, x):
        x = (x - self.param1) / self.param2
        return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0

    def gen_data(self):
        raise NotImplementedError

    def get_file_name(self, suffix='.jpg'):
        x = random.randint(1, 10000)
        file_name = datetime.now().strftime('%m_%d_%y_%H_%M_' + str(x) + suffix)
        return static_file_path + 'calc_pictures/' + file_name

    def get_big_f_simulate(self, l, r):
        cnt = 100
        ans = 0
        width = (r - l) / cnt
        for i in range(cnt):
            ans += ((self.f(l + width * i) + self.f(l + width * (i + 0.5)) * 4
                    + self.f(l + width * (i + 1))) / 6) * width
        return ans


class Test1(BaseModel):

    def __init__(self):
        super().__init__()
        self.a = 2
        self.b = 3

    def gen_data(self):
        data = dict()
        values = dict()
        values['c'] = self.a * self.b
        data['values'] = values
        return data


# 批发价格契约 采用正态分布
class WholesalePriceContract(BaseModel):

    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.p = 1
        self.cs = 0.5
        self.cr = 0.25
        self.gs = 0.2
        self.gr = 0.2
        self.v = 0
        self.w = 0.6

    def get_s(self, q):
        cnt = 100
        ans = q
        for i in range(cnt):
            ans -= q / cnt * (self.f(i * q / cnt) + self.f((i + 0.5) * q / cnt) * 4 +
                              self.f((i + 1) * q / cnt)) / 6
        return ans

    def calc(self, q, q0):
        ans = dict()
        ans['qstar'] = q
        sq = self.get_s(q)
        ans['sq'] = sq
        ans['pir'] = (self.p - self.v + self.gr) * sq - (self.cr - self.v) * q \
                     - self.gr * self.param1 - self.w * q
        ans['pis'] = self.gs * sq + (self.w - self.cs) * q - self.gs * self.param1
        ans['pi'] = ans['pir'] + ans['pis']
        ans['q0'] = q0
        ans['piq0'] = (self.p - self.v + self.gr + self.gs) * self.get_s(ans['q0']) \
                        - (self.cr + self.cs - self.v) * ans['q0'] - (self.gr + self.gs) * self.param1
        return ans

    def get_pi_for_q(self):
        x = list()
        n = 20
        for i in range(n):
            q = self.f_ni((i + 1) / (n + 1))
            x.append(q)
        y1 = list()
        y2 = list()
        y3 = list()
        y4 = list()
        for i in range(n):
            values = self.calc(x[i], x[i])
            y1.append(values['pi'])
            y2.append(values['piq0'])
            y3.append(values['pis'])
            y4.append(values['pir'])
        plt.plot(x, y1, label='total')
        # plt.plot(x, y2, label='cooperate')
        plt.plot(x, y3, label='supplier')
        plt.plot(x, y4, label='retailer')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('π-q')
        plt.xlabel('q')
        plt.ylabel('π')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        qstar = self.f_ni(1 - (self.w + self.cr - self.v) / (self.p - self.v + self.gr))
        q0 = self.f_ni(1 - (self.cs + self.cr - self.v) / (self.p - self.v + self.gr + self.gs))
        data['values'] = self.calc(qstar, q0)
        pic_list = list()
        pic_list.append(self.get_pi_for_q())
        data['urls'] = pic_list
        return data


# 回购契约
class BuybackContract(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.p = 1
        self.cs = 0.5
        self.cr = 0.25
        self.gs = 0.2
        self.gr = 0.2
        self.v = 0
        self.w = 0.6
        self.b = 0.6

    def get_s(self, q):
        cnt = 100
        ans = q
        for i in range(cnt):
            ans -= q / cnt * (self.f(i * q / cnt) + self.f((i + 0.5) * q / cnt) * 4 +
                              self.f((i + 1) * q / cnt)) / 6
        return ans

    def calc(self, q):
        ans = dict()
        ans['qstar'] = q
        sq = self.get_s(q)
        ans['sq'] = sq
        ans['wbb'] = self.b + self.cs - \
                       (self.cs + self.cr - self.v) * (self.b + self.gs) / (self.p - self.v + self.gs + self.gr)
        ans['pir'] = (self.p - self.v + self.gr - self.b) * sq - (self.cr - self.v + ans['wbb'] - self.b) * q \
                     - self.gr * self.param1
        ans['pis'] = (self.gs + self.b) * sq - (self.cs - ans['wbb'] + self.b) * q - self.gs * self.param1
        ans['pi'] = ans['pir'] + ans['pis']
        return ans

    def get_pi_for_q(self):
        x = list()
        n = 20
        for i in range(n):
            q = self.f_ni((i + 1) / (n + 1))
            x.append(q)
        y1 = list()
        y3 = list()
        y4 = list()
        for i in range(n):
            values = self.calc(x[i])
            y1.append(values['pi'])
            y3.append(values['pis'])
            y4.append(values['pir'])

        plt.plot(x, y1, label='total')
        # plt.plot(x, y2, label='cooperate')
        plt.plot(x, y3, label='supplier')
        plt.plot(x, y4, label='retailer')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('π-q')
        plt.xlabel('q')
        plt.ylabel('π')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        qstar = self.f_ni(1 - (self.cs + self.cr - self.v) / (self.p - self.v + self.gr + self.gs))
        data['values'] = self.calc(qstar)
        pic_list = list()
        pic_list.append(self.get_pi_for_q())
        data['urls'] = pic_list
        return data


# 收益共享契约
class RevenueSharingContract(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.p = 1
        self.cs = 0.5
        self.cr = 0.25
        self.gs = 0.2
        self.gr = 0.2
        self.v = 0
        self.w = 0.6
        self.phi = 0.4

    def get_s(self, q):
        cnt = 100
        ans = q
        for i in range(cnt):
            ans -= q / cnt * (self.f(i * q / cnt) + self.f((i + 0.5) * q / cnt) * 4 +
                              self.f((i + 1) * q / cnt)) / 6
        return ans

    def calc(self, q):
        ans = dict()
        ans['qstar'] = q
        sq = self.get_s(q)
        ans['sq'] = sq
        ans['wr'] = -self.cr + self.phi * self.v + (self.cs + self.cr - self.v) * \
                    (self.phi * (self.p - self.v) + self.gr) / (self.p - self.v + self.gs + self.gr)
        ans['pi'] = (self.p - self.v + self.gr + self.gs) * self.get_s(q) \
                        - (self.cr + self.cs - self.v) * q - (self.gr + self.gs) * self.param1
        ans['pir'] = (self.phi * (self.p - self.v) + self.gr) * sq - \
                     (ans['wr'] + self.cr - self.phi * self.v) * q - self.gr * self.param1
        ans['pis'] = ans['pi'] - ans['pir']
        return ans

    def get_pi_for_q(self):
        x = list()
        n = 20
        for i in range(n):
            q = self.f_ni((i + 1) / (n + 1))
            x.append(q)
        y1 = list()
        y3 = list()
        y4 = list()
        for i in range(n):
            values = self.calc(x[i])
            y1.append(values['pi'])
            y3.append(values['pis'])
            y4.append(values['pir'])
        plt.plot(x, y1, label='total')
        # plt.plot(x, y2, label='cooperate')
        plt.plot(x, y3, label='supplier')
        plt.plot(x, y4, label='retailer')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('π-q')
        plt.xlabel('q')
        plt.ylabel('π')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        qstar = self.f_ni(1 - (self.cs + self.cr - self.v) / (self.p - self.v + self.gr + self.gs))
        data['values'] = self.calc(qstar)
        pic_list = list()
        pic_list.append(self.get_pi_for_q())
        data['urls'] = pic_list
        return data


# 数量柔性契约
class QuantityFlexibleContract(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.p = 1
        self.cs = 0.5
        self.cr = 0.25
        self.gs = 0.2
        self.gr = 0.2
        self.v = 0
        self.w = 0.6
        self.ipsilong = 0.15

    def get_s(self, q):
        cnt = 100
        ans = q
        for i in range(cnt):
            ans -= q / cnt * (self.f(i * q / cnt) + self.f((i + 0.5) * q / cnt) * 4 +
                              self.f((i + 1) * q / cnt)) / 6
        return ans

    def calc(self, q):
        ans = dict()
        ans['qstar'] = q
        sq = self.get_s(q)
        ans['sq'] = sq
        ans['wq'] = (self.p - self.v + self.gr) * (1 - self.f(q)) / \
                    (1 - self.f(q) + (1 - self.ipsilong) * self.f((1 - self.ipsilong) * q)) \
                    - self.cr + self.v
        ans['pi'] = (self.p - self.v + self.gr + self.gs) * self.get_s(q) \
                    - (self.cr + self.cs - self.v) * q - (self.gr + self.gs) * self.param1
        ans['pir'] = (self.p - self.v + self.gr) * sq - (ans['wq'] + self.cr - self.v) * q \
                    + (ans['wq'] + self.cr - self.v) * self.get_big_f_simulate((1 - self.ipsilong) * q, q) \
                    - self.param1 * self.gr
        ans['pis'] = ans['pi'] - ans['pir']
        return ans

    def get_pi_for_q(self):
        x = list()
        n = 20
        for i in range(n):
            q = self.f_ni((i + 1) / (n + 1))
            x.append(q)
        y1 = list()
        y3 = list()
        y4 = list()
        for i in range(n):
            values = self.calc(x[i])
            y1.append(values['pi'])
            y3.append(values['pis'])
            y4.append(values['pir'])
        plt.plot(x, y1, label='total')
        # plt.plot(x, y2, label='cooperate')
        plt.plot(x, y3, label='supplier')
        plt.plot(x, y4, label='retailer')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('π-q')
        plt.xlabel('q')
        plt.ylabel('π')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        qstar = self.f_ni(1 - (self.cs + self.cr - self.v) / (self.p - self.v + self.gr + self.gs))
        data['values'] = self.calc(qstar)
        pic_list = list()
        pic_list.append(self.get_pi_for_q())
        data['urls'] = pic_list
        return data


# 销售回扣契约
class SaleRebateContract(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.p = 1
        self.cs = 0.5
        self.cr = 0.25
        self.gs = 0.2
        self.gr = 0.2
        self.v = 0
        self.w = 0.6
        self.t = 0.85
        self.r = 0.5

    def get_s(self, q):
        cnt = 100
        ans = q
        for i in range(cnt):
            ans -= q / cnt * (self.f(i * q / cnt) + self.f((i + 0.5) * q / cnt) * 4 +
                              self.f((i + 1) * q / cnt)) / 6
        return ans

    def calc(self, q):
        ans = dict()
        ans['qstar'] = q
        sq = self.get_s(q)
        ans['sq'] = sq
        ans['wsr'] = (self.p - self.v + self.gr + self.r) * (1 - self.f(q)) - self.cr + self.v
        ans['pi'] = (self.p - self.v + self.gr + self.gs) * self.get_s(q) \
                    - (self.cr + self.cs - self.v) * q - (self.gr + self.gs) * self.param1
        ans['pir'] = ans['pi'] + self.gs * (self.param1 - sq + q * (1 - self.f(q))) \
                    + self.r * (q * self.f(q) - self.t * q - self.get_big_f_simulate(self.t * q, q))
        ans['pis'] = ans['pi'] - ans['pir']
        return ans

    def get_pi_for_q(self):
        x = list()
        n = 20
        for i in range(n):
            q = self.f_ni((i + 1) / (n + 1))
            x.append(q)
        y1 = list()
        y3 = list()
        y4 = list()
        for i in range(n):
            values = self.calc(x[i])
            y1.append(values['pi'])
            y3.append(values['pis'])
            y4.append(values['pir'])

        plt.plot(x, y1, label='total')
        # plt.plot(x, y2, label='cooperate')
        plt.plot(x, y3, label='supplier')
        plt.plot(x, y4, label='retailer')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('π-q')
        plt.xlabel('q')
        plt.ylabel('π')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        qstar = self.f_ni(1 - (self.cs + self.cr - self.v) / (self.p - self.v + self.gr + self.gs))
        data['values'] = self.calc(qstar)
        pic_list = list()
        pic_list.append(self.get_pi_for_q())
        data['urls'] = pic_list
        return data


# 数量折扣契约
class QuantityDiscountContract(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.p = 1
        self.cs = 0.5
        self.cr = 0.25
        self.gs = 0.2
        self.gr = 0.2
        self.v = 0
        self.w = 0.6
        self.lamda = 0.5

    def get_s(self, q):
        cnt = 100
        ans = q
        for i in range(cnt):
            ans -= q / cnt * (self.f(i * q / cnt) + self.f((i + 0.5) * q / cnt) * 4 +
                              self.f((i + 1) * q / cnt)) / 6
        return ans

    def calc(self, q):
        ans = dict()
        ans['qstar'] = q
        sq = self.get_s(q)
        ans['sq'] = sq
        ans['wdq'] = ((1 - self.lamda) * (self.p - self.v + self.gs + self.gr) - self.gs) * sq / q + \
                       self.lamda * (self.cs + self.cr - self.v) - self.cr + self.v
        ans['pi'] = (self.p - self.v + self.gr + self.gs) * self.get_s(q) \
                    - (self.cr + self.cs - self.v) * q - (self.gr + self.gs) * self.param1
        ans['pir'] = self.lamda * (ans['pi'] + (self.gs + self.gr) * self.param1) - self.gr * self.param1
        ans['pis'] = ans['pi'] - ans['pir']
        return ans

    def get_pi_for_q(self):
        x = list()
        n = 20
        for i in range(n):
            q = self.f_ni((i + 1) / (n + 1))
            x.append(q)
        y1 = list()
        y3 = list()
        y4 = list()
        for i in range(n):
            values = self.calc(x[i])
            y1.append(values['pi'])
            y3.append(values['pis'])
            y4.append(values['pir'])

        plt.plot(x, y1, label='total')
        # plt.plot(x, y2, label='cooperate')
        plt.plot(x, y3, label='supplier')
        plt.plot(x, y4, label='retailer')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('π-q')
        plt.xlabel('q')
        plt.ylabel('π')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        qstar = self.f_ni(1 - (self.cs + self.cr - self.v) / (self.p - self.v + self.gr + self.gs))
        data['values'] = self.calc(qstar)
        pic_list = list()
        pic_list.append(self.get_pi_for_q())
        data['urls'] = pic_list
        return data


# NIM游戏
class NimGame(BaseModel):
    def __init__(self):
        super().__init__()
        self.x1 = 2
        self.x2 = 3
        self.x3 = 5

    def gen_data(self):
        data = dict()
        self.x1 = int(self.x1)
        self.x2 = int(self.x2)
        self.x3 = int(self.x3)
        if (self.x1 ^ self.x2 ^ self.x3) == 0:
            winner = 'Bob'
        else:
            winner = 'Alice'
        data['winner'] = winner
        return data


# 关键字搜索拍卖
class KeywordSearchAuction(BaseModel):
    def __init__(self):
        super().__init__()
        self.alpha1 = 2
        self.alpha2 = 3
        self.b1 = 3
        self.b2 = 5
        self.b3 = 8

    def set_data(self, data, pos, x, p):
        if pos == 1:
            data['x1'] = x
            data['p1'] = p
        elif pos == 2:
            data['x2'] = x
            data['p2'] = p
        elif pos == 3:
            data['x3'] = x
            data['p3'] = p

    def gen_data(self):
        data = dict()
        b = list()
        b.append([self.b1, 1])
        b.append([self.b2, 2])
        b.append([self.b3, 3])
        b.sort(key=lambda x: -x[0])
        alpha = list()
        alpha.append([self.alpha1, 1])
        alpha.append([self.alpha2, 2])
        alpha.sort(key=lambda x: -x[0])
        b[0].append(alpha[0][1])
        b[0].append(b[1][0] * (alpha[0][0] - alpha[1][0]) + b[2][0] * alpha[1][0])
        b[1].append(alpha[1][1])
        b[1].append(b[2][0] * alpha[1][0])
        b[2].append(0)
        b[2].append(0)
        for i in range(3):
            self.set_data(data, b[i][1], b[i][2], b[i][3])
        return data


# 整数01背包问题
class IntegerKnapsackProblem(BaseModel):
    def __init__(self):
        super().__init__()
        self.V = 9
        self.v1 = 3
        self.v2 = 4
        self.v3 = 5
        self.w1 = 8
        self.w2 = 10
        self.w3 = 12

    def gen_data(self):
        self.V = int(self.V)
        self.v1 = int(self.v1)
        self.v2 = int(self.v2)
        self.v3 = int(self.v3)
        data = dict()
        v = [self.v1, self.v2, self.v3]
        w = [self.w1, self.w2, self.w3]
        x = [0] * 3
        dp = [[0 for _ in range(self.V + 1)] for _ in range(4)]
        sol = [[0 for _ in range(self.V + 1)] for _ in range(4)]
        for i in range(1, 4):
            for j in range(self.V + 1):
                if j >= v[i - 1] and dp[i - 1][j] < dp[i - 1][j - v[i - 1]] + w[i - 1]:
                    dp[i][j] = dp[i - 1][j - v[i - 1]] + w[i - 1]
                    sol[i][j] = j - v[i - 1]
                else:
                    dp[i][j] = dp[i - 1][j]
                    sol[i][j] = j
        now = self.V
        pos = 3
        while pos > 0:
            if sol[pos][now] != now:
                x[pos - 1] = 1
            else:
                x[pos - 1] = 0
            now = sol[pos][now]
            pos -= 1
        for i in range(3):
            data['x' + str(i + 1)] = x[i]
        data['W'] = dp[3][self.V]
        return data


# 生产博弈算例
class ProductionGame(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.a = 15
        self.b = 0.1
        self.c = 1

    def calc(self):
        left = 0
        right = 1000000
        while left + 0.0001 < right:
            mid = (left + right) / 2
            if self.L(mid) > 0:
                left = mid
            else:
                right = mid
        return left

    def L(self, q):
        return self.a * math.exp(-self.b * 2 * q) - self.c - q * self.a * self.b * math.exp(-self.b * 2 * q)

    def get_L_for_qn(self, l, r):
        x = list()
        cnt = 100
        for i in range(cnt):
            x.append(l + i * (r - l) / cnt)
        y1 = list()
        for i in range(cnt):
            value = self.L(x[i])
            y1.append(value)

        plt.plot(x, y1, label='L(q)')
        # plt.plot(x, y2, label='cooperate')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('L-q')
        plt.xlabel('q')
        plt.ylabel('L')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        data['qn'] = self.calc()
        pic_list = list()
        pic_list.append(self.get_L_for_qn(0, data['qn'] * 2))
        data['urls'] = pic_list
        return data


# 价格博弈算例
class PricingGame(BaseModel):
    def __init__(self):
        super().__init__()
        self.set_f(mode=1, params=[100, 20])
        self.a = 15
        self.b = 2
        self.c = 1
        self.alpha = 0.5

    def calc(self):
        left = 0
        right = 1000000
        while left + 0.0001 < right:
            mid = (left + right) / 2
            if (self.react(mid, 1) - self.react(mid, 2)) * (self.react(left, 1) - self.react(left, 2)) > 0:
                left = mid
            else:
                right = mid
        return left, self.react(left, 1)

    def react(self, q, mode):
        left = 0
        right = 1000000
        while left + 0.0001 < right:
            mid = (left + right) / 2
            if mode == 1:
                value = self.L1(q, mid)
            else:
                value = self.L2(q, mid)
            if value < 0:
                right = mid
            else:
                left = mid
        return left

    def L1(self, w, m):
        return self.a - self.b * (w + m) ** self.alpha - m * self.alpha * (w + m) ** (self.alpha - 1)

    def L2(self, w, m):
        return self.a - self.b * (w + m) ** self.alpha - (w - self.c) * self.alpha * (w + m) ** (self.alpha - 1)

    def get_two_actions(self, l, r):
        x = list()
        cnt = 100
        for i in range(cnt):
            x.append(l + i * (r - l) / cnt)
        y1 = list()
        y2 = list()
        for i in range(cnt):
            value = self.react(x[i], 1)
            y1.append(value)
            value = self.react(x[i], 2)
            y2.append(value)

        plt.plot(x, y1, label='retailer')
        plt.plot(x, y2, label='supplier')
        plt.legend()
        file_name = self.get_file_name()
        plt.title('w-m reaction picture')
        plt.xlabel('w')
        plt.ylabel('m')
        plt.savefig(file_name)
        plt.close()
        return file_name

    def gen_data(self):
        data = dict()
        wstar, mstar = self.calc()
        data['wstar'] = wstar
        data['mstar'] = mstar
        pic_list = list()
        pic_list.append(self.get_two_actions(0, wstar * 2))
        data['urls'] = pic_list
        return data


class ModelManager:
    def work(self, request):
        try:
            json_dict = json.loads(request.body)
        except Exception:
            return JsonResponse({'code': 500, 'message': '请求参数错误'})
        example_key = json_dict.get('example_key', None)
        param = json_dict.get('param', None)
        if example_key is None or param is None:
            return JsonResponse({'code': 500, 'message': '请求参数错误'})
        try:
            example = Example.objects.get(id=example_key)
        except Exception:
            return JsonResponse({'code': 500, 'message': '请求参数错误'})
        classname = example.mymodel.classname
        if classname == 'Test1':
            cla = Test1()
        elif classname == 'WholesalePriceContract':
            cla = WholesalePriceContract()
        elif classname == 'BuybackContract':
            cla = BuybackContract()
        elif classname == 'RevenueSharingContract':
            cla = RevenueSharingContract()
        elif classname == 'QuantityFlexibleContract':
            cla = QuantityFlexibleContract()
        elif classname == 'SaleRebateContract':
            cla = SaleRebateContract()
        elif classname == 'QuantityDiscountContract':
            cla = QuantityDiscountContract()
        elif classname == 'ProductionGame':
            cla = ProductionGame()
        elif classname == 'PricingGame':
            cla = PricingGame()
        elif classname == 'IntegerKnapsackProblem':
            cla = IntegerKnapsackProblem()
        elif classname == 'KeywordSearchAuction':
            cla = KeywordSearchAuction()
        elif classname == 'NimGame':
            cla = NimGame()
        else:
            raise TypeError
        cla.set_value(param)
        data = cla.gen_data()
        return JsonResponse({'code': 200, 'message': '请求成功', 'data': data})
